package com.wondertek.poms.core.filter;


import org.apache.commons.lang3.StringUtils;

import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.IRulePo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BaseRuleFilter implements IRuleFilter {

	public abstract void doFilter(IRulePo rule, Content c, RuleFilterChain chain);
	
	/**
	 * <p><b>Title:</b> baseFilter</p>
	 * <p><b>Description:</b> 规则过滤基本方法 </p>
	 * @author douzi
	 * @param ruleStr
	 * @param voStr
	 * @param chain
	 */
	protected void baseFilter(Long id, String ruleStr, String voStr, RuleFilterChain chain, String comments) {
//		log.info("规则过滤基本方法|id:" + id + "|ruleStr:" + ruleStr + "|voStr:" + voStr + "|comments:" + comments);
		if (chain.isFlag() && StringUtils.isNotBlank(ruleStr)) {
            ruleStr = StringUtils.trim(ruleStr);
			chain.setFlag(Boolean.FALSE);
			String vo = StringUtils.trim(voStr);
			if (ruleStr.equals(vo)) {
//				log.info("规则过滤基本方法|id:" + id + "|ruleStr:" + ruleStr + "|voStr:" + voStr + "|true" + "|comments:" + comments);
				chain.setFlag(Boolean.TRUE);
			}
			
			log(id, chain, comments, ruleStr, vo);
		}
	}
	
	public static void main(String[] args) {
		String ruleStr = "2,4,1,3";
		String voStr = "2|4";
		
//		specialFilter(1L, ruleStr, voStr, new RuleFilterChain(), "zhognduan");
	}
	
	/**
	 * <p><b>Title:</b> baseFilter</p>
	 * <p><b>Description:</b> 规则过滤基本方法 </p>
	 * @author douzi
	 * @param chain
	 */
	protected void numberFilter(Long id, Integer ruleInt, Integer voInt, RuleFilterChain chain, String comments) {
//		log.info("规则过滤基本方法|id:" + id + "|ruleInt:" + ruleInt + "|voInt:" + voInt + "|comments:" + comments);
		if (chain.isFlag() && ruleInt != null) {
			chain.setFlag(Boolean.FALSE);
			if (ruleInt.equals(voInt)) {
//				log.info("规则过滤基本方法|id:" + id + "|ruleInt:" + ruleInt + "|voInt:" + voInt + "|true" + "|comments:" + comments);
				chain.setFlag(Boolean.TRUE);
			}
			
			log(id, chain, comments, ruleInt, voInt);
		}
	}

	/**
	 * <p><b>Title:</b> log</p>
	 * <p><b>Description:</b> 打log日志</p>
	 * @author douzi
	 * @param id
	 * @param chain
	 * @param comments
	 * @param rule
	 * @param vo
	 */
	protected void log(Long id, RuleFilterChain chain, String comments, Object rule, Object vo) {
		if (!chain.isFlag()) {
			chain.getResultStr().append("规则:" + id + "|" +comments + "不匹配|规则" + comments + ":" + rule + "|媒资" + comments + ":" + vo);
		}
	}

	/**
	 * <p><b>Title:</b> terminalFilter</p>
	 * <p><b>Description:</b> 版权终端、版权范围、辅助分类    比较特殊,规则使用','分隔，媒资使用'|'分隔；
	 * 					eg:规则版权终端:13,12,10,9,8,11;媒资版权终端:2|3|4|5|6</p>
	 * @author douzi
	 * @param ruleStr
	 * @param voStr
	 * @param chain
	 */
	protected void specialFilter(Long id, String ruleStr, String voStr, RuleFilterChain chain, String comments) {
		if (chain.isFlag() && StringUtils.isNotBlank(ruleStr)) {
            ruleStr = ruleStr.trim();
			chain.setFlag(Boolean.FALSE);
			if (StringUtils.isNotBlank(voStr)) {
                voStr = voStr.trim();
				String ruleTemp = ruleStr.replaceAll("，", ",");
				String voTemp = voStr.replaceAll("，", ",");
				voTemp = "|" + voTemp + "|";
				String[] ruleRmds = ruleTemp.split(",");
				for (String rmd : ruleRmds) {
					rmd = "|" + rmd + "|";
					if (voTemp.contains(rmd)) {
						chain.setFlag(Boolean.TRUE);
						break;
					}
				}
			}

			log(id, chain, comments, ruleStr, voStr);
		}
	}
	
	/**
	 * <p><b>Title:</b> manyFilter</p>
	 * <p><b>Description:</b> 复杂过滤方法,规则多个、内容一个</p>
	 * @author douzi
	 * @param ruleStr
	 * @param voStr
	 * @param chain
	 */
	protected void moreFilter(Long id, String ruleStr, String voStr, RuleFilterChain chain, String comments) {
//		log.info("规则过滤基本方法|id:" + id + "|ruleStr:" + ruleStr + "|voStr:" + voStr + "|comments:" + comments);
		if (chain.isFlag() && StringUtils.isNotBlank(ruleStr)) {
            ruleStr = ruleStr.trim();
			chain.setFlag(Boolean.FALSE);
			if (StringUtils.isNotBlank(voStr)) {
                voStr = voStr.trim();
				String ruleTemp = ruleStr.replaceAll("，", ",");
				if (("," + ruleTemp + ",").contains("," + voStr + ",")) {
					chain.setFlag(Boolean.TRUE);
				}
			}

			log(id, chain, comments, ruleStr, voStr);
		}
	}
	
	/**
	 * <p><b>Title:</b> manyFilter</p>
	 * <p><b>Description:</b> 复杂过滤方法,配置中逗号分隔</p>
	 * @author douzi
	 * @param ruleStr
	 * @param voStr
	 * @param chain
	 */
	protected void manyFilter(Long id, String ruleStr, String voStr, RuleFilterChain chain, String comments) {
//		log.info("规则过滤基本方法|id:" + id + "|ruleStr:" + ruleStr + "|voStr:" + voStr + "|comments:" + comments);
		if (chain.isFlag() && StringUtils.isNotBlank(ruleStr)) {
            ruleStr = ruleStr.trim();
			chain.setFlag(Boolean.FALSE);
			if (StringUtils.isNotBlank(voStr)) {
                voStr = voStr.trim();
				String ruleTemp = ruleStr.replaceAll("，", ",");
				String voTemp = voStr.replaceAll("，", ",");
				voTemp = "," + voTemp + ",";  // 把 '电影,电视剧' 修改为 ',电影,电视剧,'
				String[] ruleRmds = ruleTemp.split(",");
				for (String rmd : ruleRmds) {
					rmd = "," + rmd + ","; // 把 '电' 修改为 ',电,' 这样就不会导致contains 一个电就符合规则了
					if (voTemp.contains(rmd)) {
//						log.info("规则过滤基本方法|id:" + id + "|ruleStr:" + ruleStr + "|voStr:" + voStr + "|true" + "|comments:" + comments);
						chain.setFlag(Boolean.TRUE);
						break;
					}
				}
			}

			log(id, chain, comments, ruleStr, voStr);
		}
	}
	
	/**
	 * <p><b>Title:</b> manyFilter</p>
	 * <p><b>Description:</b> 复杂过滤方法,配置中'|'分隔</p>
	 * @author douzi
	 * @param ruleStr
	 * @param voStr
	 * @param chain
	 */
	protected void manyVerticalFilter(Long id, String ruleStr, String voStr, RuleFilterChain chain, String comments) {
//		log.info("规则过滤基本方法|id:" + id + "|ruleStr:" + ruleStr + "|voStr:" + voStr + "|comments:" + comments);
		if (chain.isFlag() && StringUtils.isNotBlank(ruleStr)) {
            ruleStr =  ruleStr.trim();
			chain.setFlag(Boolean.FALSE);
			if (StringUtils.isNotBlank(voStr)) {
                voStr = voStr.trim();
				String voTemp = "|" + voStr + "|";
				String[] ruleTemp = ruleStr.split("，|,");
				for (String temp : ruleTemp) {
					if (voTemp.contains( "|" + temp + "|" )) {
//						log.info("规则过滤基本方法|id:" + id + "|ruleStr:" + ruleStr + "|voStr:" + voStr + "|true" + "|comments:" + comments);
						chain.setFlag(Boolean.TRUE);
						break;
					}
				}
			}
			
			log(id, chain, comments, ruleStr, voStr);
		}
	}

}
